package singleThreaded;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * After taking page text from PageParser
 * DataCatherer counts the number of pages retrived 
 * and the number of keyword repeated.
 * @author Sydney Lin
 * @version 1.0
 */
public class DataGatherer
{
 /**
  * A map of keywords and its number of hits. 
  */
  private Map<String, Integer> my_map;
  
  /**
   * The page counter.
   */
  private int my_page_counter;
  
  /**
   * The word counter.
   */
  private int my_total_words;
  
  /**
   * Text of the web page.
   */
  private String my_page_text;

  /**
   * Construct a DataGatherer object.
   * 
   * @param the_keywords The keywords.
   */
  public DataGatherer(final String[] the_keywords)
  {
    my_page_counter = 0;
    my_total_words = 0;
    my_map = new HashMap<String, Integer>();
    for (int i = 0; i < the_keywords.length; i++)
    {
      my_map.put(the_keywords[i], 0);
    }    
  }
  

  /**
   * Count keywords and total words.
   * @param the_page_text The text of this page.
   */
  public void processWords(final String the_page_text)
  {    
    my_page_text = the_page_text.toLowerCase(Locale.ENGLISH).replace("[^A-Za-z0-9]", "");
    final String[] the_word_array = my_page_text.split("[, ]+(and|or)*[, ]*");
    my_total_words = my_total_words + the_word_array.length;
    for (int i = 0; i < the_word_array.length; i++)
    {     
      if (my_map.containsKey(the_word_array[i])) 
      {
        my_map.put(the_word_array[i], my_map.get(the_word_array[i]) + 1);
      }
    }
    my_page_counter++;
  }
  
  /**
   * 
   * @return Total number of words so far.
   */
  public int getWordsNumber()
  {
    return my_total_words; 
  }
  
  /**
   * 
   * @return Total number of pages so far.
   */
  public int getPagesNumber()
  {
    return my_page_counter;    
  }
  
  /**
   * 
   * @return The map of keywords and number of hits.
   */
  public Map<String, Integer> getMapKeywordHit()
  {
    return (Map<String, Integer>) ((HashMap<String, Integer>) my_map).clone();
  }
}
